package com.jprocms.module.cms.convert.channel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.collection.CollectionUtils;
import com.jprocms.module.cms.controller.admin.channel.vo.*;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelAttrDO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelDO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelExtDO;
import com.jprocms.module.cms.dal.dataobject.model.ModelDO;
import com.jprocms.module.cms.util.ModelWrapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 栏目 Convert
 *
 * @author jprocms
 */
@Mapper
public interface ChannelConvert {

    ChannelConvert INSTANCE = Mappers.getMapper(ChannelConvert.class);

    ChannelDO convert(ChannelCreateReqVO bean);

    ChannelDO convert(ChannelUpdateReqVO bean);

    List<ChannelRespVO> convertList(List<ChannelDO> list);


    PageResult<ChannelRespVO> convertPage(PageResult<ChannelDO> page);

    List<ChannelExcelVO> convertList02(List<ChannelDO> list);

    List<ChannelSimpleRespVO> convertSimpleList(List<ChannelDO> list);

    ChannelRespVO convert(ChannelDO bean);

    default ChannelRespVO convertToResp(ChannelDO bean, ChannelExtDO channelExt, ModelDO modelDO, List<ChannelAttrDO> attrDOS) {
        ChannelRespVO respVO = convert(bean);
        ModelWrapper modelWrapper = new ModelWrapper(modelDO);
        respVO.setCustoms(modelWrapper.assembleCustoms(attrDOS));
        respVO.setPageSize(channelExt.getPageSize());
        respVO.setListPage(channelExt.getListPage());
        respVO.setLinkTarget(channelExt.getLinkTarget());
        respVO.setCommentStatus(channelExt.getCommentStatus());
        respVO.setSearchStatus(bean.getSearchStatus());
        respVO.setContributeStatus(bean.getContributeStatus());
        respVO.setContentTplPc(channelExt.getContentTplPc());
        respVO.setContentTplMobile(channelExt.getContentTplMobile());
        respVO.setTplPc(channelExt.getTplPc());
        respVO.setTplMobile(channelExt.getTplMobile());
        respVO.setListImage(channelExt.getListImage());
        respVO.setOutLinkUrl(channelExt.getOutLinkUrl());
        respVO.setSeoDescription(channelExt.getSeoDescription());
        respVO.setSeoKeyword(channelExt.getSeoKeyword());
        respVO.setSeoTitle(channelExt.getSeoTitle());
        respVO.setCollectRule(channelExt.getCollectRule());
        respVO.setTxt(channelExt.getTxt());
        respVO.setUseProcessDefinitionId(bean.getUseProcessDefinitionId());
        return respVO;
    }

    default ChannelDO convertToChannel(String channelName, Long modelId, Long siteId) {
        ChannelDO channel = new ChannelDO();
        channel.setModelId(modelId);
        channel.setName(channelName);
        channel.setModelId(modelId);
        channel.setPath(PinyinUtil.getPinyin(channelName));
        channel.setSiteId(siteId);
        channel.init();
        return channel;
    }

    ChannelListRespVO convertToLstResp(ChannelDO bean);

    List<ChannelNodeVo> convertToNodeList(List<ChannelDO> list);

    default ChannelListRespVO convertToLstRespVo(ChannelDO bean) {
        ChannelListRespVO vo = convertToLstResp(bean);
        vo.setNodeList(convertToNodeList(bean.getNodeList()));
        return vo;
    }

    default List<ChannelListRespVO> convertListResp(List<ChannelDO> list, Map<Long, String> modelIdNameMap) {
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<ChannelListRespVO> channelListRespVOS = CollectionUtils.convertList(list, this::convertToLstRespVo);
        channelListRespVOS.forEach(c -> {
            c.setModelName(modelIdNameMap.get(c.getModelId()));
        });
        return channelListRespVOS;
    }

    ChannelViewListRespVO convertToViewListResp(ChannelDO bean);

    default ChannelViewListRespVO convertToViewListRespVo(ChannelDO bean) {
        ChannelViewListRespVO vo = convertToViewListResp(bean);
        vo.setNodeList(convertToNodeList(bean.getNodeList()));
        return vo;
    }

    default List<ChannelViewListRespVO> convertViewListResp(List<ChannelDO> list) {
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<ChannelViewListRespVO> channelListRespVOS = CollectionUtils.convertList(list, this::convertToViewListRespVo);
        return channelListRespVOS;
    }

    @Mapping(source = "ext.txt", target = "txt")
    @Mapping(source = "ext.seoTitle", target = "seoTitle")
    @Mapping(source = "ext.seoDescription", target = "seoDescription")
    @Mapping(source = "ext.seoKeyword", target = "seoKeyword")
    @Mapping(source = "ext.linkTarget", target = "linkTarget")
    @Mapping(source = "ext.outLinkUrl", target = "outLinkUrl")
    ChannelCreateReqVO convert(ChannelDO bean,ChannelExtDO ext);

}
